<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2021/5/31
  Time: 11:59
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/common/common.jsp" %>
<html>
<head>
    <title>Title</title>
    <script>
        $(function () {
            $('#table').bootstrapTable({
                url: '<%=path%>/role/getInfo',
                method: 'post',                      //请求方式（*）
                contentType: "application/x-www-form-urlencoded",
                //toolbar: '#for_div',              //工具按钮用哪个容器
                striped: true,                     //是否显示行间隔色
                cache: true, //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）

                minimumCountColumns: 2,             //最少允许的列数
                clickToSelect: true,                //是否启用点击选中行
                uniqueId: "rid",                     //每一行的唯一标识，一般为主键列
                showToggle: true,                   //是否显示详细视图和列表视图的切换按钮
                // cardView: true,                    //是否显示详细视图
                detailView: true,                  //是否显示父子表
                //列

                columns: [{
                    checkbox: true,
                    visible: true                  //是否显示复选框
                }, {
                    field: 'rid',
                    title: 'ID'
                }, {
                    field: 'rname',
                    title: '角色名字'
                }, {
                    field: 'rid',
                    title: "操作",
                    //value daibiaozhi row 代表 数据 index 代表当前下表
                    formatter: function (value, row, index) {
                        var id = row.rid;
                        var result = "";
                        result += "<a href='javascript:toUpdate(" + id + ")' class='btn btn-xs blue' title='编辑'><span class='glyphicon glyphicon-pencil'></span></a>";
                        result += "<a href='javascript:deleteInfoById(" + id + ")' class='btn btn-xs red' title='删除'><span class='glyphicon glyphicon-remove'></span></a>";
                        return result;
                    }

                },]

            });

        })


        function deleteInfoById(id) {
            alert(id)
            if (confirm("确认删除吗")) {

                $.ajax({
                    url: "<%=path%>/role/roledelete",
                    data: {rid: id},
                    dataType: "json",
                    type: "post",
                    success: function (result) {
                        alert("删除成功");
                        $("#table").bootstrapTable("refresh");

                    },
                    error: function (result) {
                        console.log(result);
                        alert("网络异常请刷新");

                    }


                })
            }
        };

        function toUpdate(id) {
            BootstrapDialog.show({
                title: '修改',
                message: $('<div></div>').load('<%=path%>/role/huixuan?rid=' + id),
                closeable: true,
                buttons: [{
                    label: '取消',
                    action: function (dialog) {
                        dialog.close();
                    }

                }, {
                    label: '确定',
                    action: function (dialog) {
                        var mids = [];//
                        var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
                        var nodes = treeObj.getCheckedNodes(true);
                        for (var i = 0; i < nodes.length; i++) {
                            mids.push(nodes[i].id);
                        }
                        if (nodes.length == 0) {
                            alert("请选择权限");

                        }

                        $.ajax({
                            url: "<%=path%>/role/updateInfo",
                            type: "post",
                            dataType: "json",
                            data: $("#update_form").serialize() + "&mids=" + mids,
                            success: function (data) {
                                toastr.success("增加成功")
                                $("#table").bootstrapTable("refresh");

                                dialog.close();


                            },
                            error: function (data) {
                                console.log(data);
                                alert("网络异常");
                            }

                        })

                    }
                }
                ]

            })
        }

        function insertInfo() {
            BootstrapDialog.show({
                title: '增加',
                message: $('<div></div>').load('<%=path%>/role/addInfo'),
                closeable: true,
                buttons: [{
                    label: '取消',
                    action: function (dialog) {
                        dialog.close();
                    }

                }, {
                    label: '确定',
                    action: function (dialog) {
                        //获取id获取输入框被勾选 或 未勾选的节点集合。[setting.check.enable = true 时有效]
                        //通过遍历拿到子节点的id 因为是多和 所以用集合接受 id 就是 主键id
                        var mids = [];//
                        var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
                        var nodes = treeObj.getCheckedNodes(true);
                        for (var i = 0; i < nodes.length; i++) {
                            mids.push(nodes[i].id);
                            alert(mids);

                        }
                        console.log(nodes)
                        alert(mids);
                        $.ajax({
                            url: "<%=path%>/role/insertinto",
                            type: "post",
                            dataType: "json",
                            data: $("#add_form").serialize() + "&mids=" + mids,

                            success: function (data) {
                                $("#table").bootstrapTable("refresh");
                                toastr.success("增加成功")
                                dialog.close();
                            },
                            error: function (data) {
                                console.log(data);
                                alert("网络异常");
                            }

                        })

                    }
                }
                ]

            })
        }


    </script>
</head>
<body>
<input type="button" value="增加" onclick="insertInfo()">
<table id="table"></table>
</body>
</html>
